package com.learn.project2.utils;


import lombok.Data;

@Data
public class Result<T> {
    T data;
    String errmsg;//告诉前端请求的消息
    long errno;//自己的前后端应用 ： 自定义的状态码 → 通常前端根据该状态码做不同的处理

    public static Result ok(){
        Result result = new Result();
        result.setErrno(0);
        result.setErrmsg("成功");
        return result;
    }
    public static <T> Result ok(T data){
        Result<T> result = new Result<>();
        result.setErrno(0);
        result.setData(data);
        result.setErrmsg("成功");
        return result;
    }
    /**
     * @description:  可以返回信息的ok
     * @param:
     * @return:
     * @author 岳子昂
     * @date: 2022-01-10 23:37
     */
    public static <T> Result ok(String msg){
        Result<T> result = new Result<>();
        result.setErrno(0);
        result.setErrmsg(msg);
        return result;
    }
    public static <T> Result ok(T data, String msg){
        Result<T> result = new Result<>();
        result.setData(data);
        result.setErrno(0);
        result.setErrmsg("成功");
        return result;
    }
    public static <T> Result fail(){
        Result<T> result = new Result<>();
        result.setErrno(500);
        result.setErrmsg("失败");
        return result;
    }
    public static <T> Result fail(String msg){
        Result<T> result = new Result<>();
        result.setErrno(500);
        result.setErrmsg(msg);
        return result;
    }

    public static <T> Result fail2(String msg){
        Result<T> result = new Result<>();
        result.setErrno(501);
        result.setErrmsg(msg);
        return result;
    }

    /**
     * @description:  可以自主设置错误码的fail
     * @param:
     * @return:
     * @author 岳子昂
     * @date: 2022-01-10 23:38
     */
    public static <T> Result fail(Integer errno ,String msg){
        Result<T> result = new Result<>();
        result.setErrno(errno);
        result.setErrmsg(msg);
        return result;
    }
}
